package a10_动态规划;

import java.util.Arrays;

/**
 * <p>
 * a04_使用最小花费爬楼梯
 * </p>
 *
 * @author flyduck
 * @since 2025/1/21
 */
public class a04_使用最小花费爬楼梯 {

    public static void main(String[] args) {
        a04_使用最小花费爬楼梯 test = new a04_使用最小花费爬楼梯();
        int i = test.minCostClimbingStairs(new int[]{10, 15, 20});
//        int i = test.minCostClimbingStairs(new int[]{1,100,1,1,1,100,1,1,100,1});
        System.out.println();
    }

    //dp[i]数组含义：到达第i个台阶花费最少体力
    //第一种：到达第i个台阶花费最少体力 = 到达第i-1，然后跳1步 = dp[i-1] + cost[i-1]
    //第二种：到达第i个台阶花费最少体力 = 到达第i-2，然后跳2步 = dp[i-2] + cost[i-2]
    //递推公式：dp[i] = Math.min(dp[i-1] + cost[i-1],dp[i-2] + cost[i-2])
    //遍历方式：dp[0] = 0 dp[1] = 0
    public int minCostClimbingStairs(int[] cost) {
        int[] dp = new int[cost.length + 1];
        dp[0] = 0;
        dp[1] = 0;
        for (int i = 2; i < dp.length; i++) {
            dp[i] = Math.min(dp[i-1] + cost[i-1],dp[i-2] + cost[i-2]);
        }
        return dp[dp.length-1];
    }
}
